﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DevExpress.Web.ASPxGridView;
using RR;

public partial class Default2 : System.Web.UI.Page
{
    protected RegistRar regrar = new RegistRar();

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void grd_room_BeforePerformDataSelect(object sender, EventArgs e)
    {
        Session["ARCOBJECT"] = (sender as ASPxGridView).GetMasterRowKeyValue();
    }

    protected void grd_arcObject_RowInserting(object sender, DevExpress.Web.Data.ASPxDataInsertingEventArgs e)
    {
        if (e.NewValues["CODE"] == null)
            e.NewValues["CODE"] = regrar.Gen_id("ARCOBJECT_gen");
    }
    protected void grd_room_RowInserting(object sender, DevExpress.Web.Data.ASPxDataInsertingEventArgs e)
    {
        if (e.NewValues["CODE"] == null)
            e.NewValues["CODE"] = regrar.Gen_id("ARCROOM_gen");
        if (e.NewValues["ARCOBJECT"] == null)
            e.NewValues["ARCOBJECT"] = Session["ARCOBJECT"];
    }

    protected void grd_ArcRack_BeforePerformDataSelect(object sender, EventArgs e)
    {
        Session["ARCROOM"] = (sender as ASPxGridView).GetMasterRowKeyValue();
    }

    protected void grd_ArcRack_RowInserting(object sender, DevExpress.Web.Data.ASPxDataInsertingEventArgs e)
    {
        int num = Convert.ToInt32(e.NewValues["NOMER"]);
        if (isNuExists(num, Convert.ToInt32(Session["ARCROOM"])))
            throw new Exception("Стелаж с данным номером уже существует в базе данных!");

        if (e.NewValues["CODE"] == null)
            e.NewValues["CODE"] = regrar.Gen_id("ARCRACK_gen");
        if (e.NewValues["ARCROOM"] == null)
            e.NewValues["ARCROOM"] = Session["ARCROOM"];
    }

    /// <summary>
    /// Возвращает true если номер найден в базе
    /// </summary>
    /// <param name="num"></param>
    /// <returns></returns>
    private bool isNuExists(int num, int room)
    {
        var query = (from r in regrar.ARCRACK
                     where r.NOMER == num && r.ARCROOM == room
                     select r).FirstOrDefault();
        return (query != null);
    }

    #region удаление стелажей
    protected void grd_ArcRack_RowDeleting(object sender, DevExpress.Web.Data.ASPxDataDeletingEventArgs e)
    {
        int code = Convert.ToInt32(e.Values["CODE"]);
        var query = (from r in regrar.ARCPLACE
                     where r.ARCRACK == code && r.STATE != 0
                     select r).FirstOrDefault();
        if (query != null)
            throw new Exception("На стеллаже есть папки с документами. Удалять нельзя!");
    }
    #endregion

    #region удаление помещений
    protected void grd_room_RowDeleting(object sender, DevExpress.Web.Data.ASPxDataDeletingEventArgs e)
    {
        int code = Convert.ToInt32(e.Values["CODE"]);
        var query = (from r in regrar.ARCRACK
                     where r.ARCROOM == code
                     select r).FirstOrDefault();
        if (query != null)
            throw new Exception("В помещении имеются стелажи. Удалять нельзя!");
    }
    #endregion

    #region удаление объектов
    protected void grd_arcObject_RowDeleting(object sender, DevExpress.Web.Data.ASPxDataDeletingEventArgs e)
    {
        int code = Convert.ToInt32(e.Values["CODE"]);
        var query = (from r in regrar.ARCROOM
                     where r.ARCOBJECT == code
                     select r).FirstOrDefault();
        if (query != null)
            throw new Exception("На объекте имеются помещения. Удалять нельзя!");
    }
    #endregion
}